package cn.com.guage.dtm.metadata.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.metadata.domain.MdFunction;
import cn.com.guage.dtm.metadata.service.IMdFunctionService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 内置函数Controller
 * 
 * @author liujun
 * @date 2021-07-08
 */
@Controller
@RequestMapping("/metadata/function")
public class MdFunctionController extends BaseController
{
    private String prefix = "metadata/function";

    @Autowired
    private IMdFunctionService mdFunctionService;

    @RequiresPermissions("metadata:function:view")
    @GetMapping()
    public String function()
    {
        return prefix + "/function";
    }

    /**
     * 查询内置函数列表
     */
    @RequiresPermissions("metadata:function:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdFunction mdFunction)
    {
        startPage();
        List<MdFunction> list = mdFunctionService.selectMdFunctionList(mdFunction);
        return getDataTable(list);
    }

    /**
     * 导出内置函数列表
     */
    @RequiresPermissions("metadata:function:export")
    @Log(title = "内置函数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdFunction mdFunction)
    {
        List<MdFunction> list = mdFunctionService.selectMdFunctionList(mdFunction);
        ExcelUtil<MdFunction> util = new ExcelUtil<MdFunction>(MdFunction.class);
        return util.exportExcel(list, "内置函数数据");
    }

    /**
     * 新增内置函数
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存内置函数
     */
    @RequiresPermissions("metadata:function:add")
    @Log(title = "内置函数", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdFunction mdFunction)
    {
        return toAjax(mdFunctionService.insertMdFunction(mdFunction));
    }

    /**
     * 修改内置函数
     */
    @GetMapping("/edit/{functionId}")
    public String edit(@PathVariable("functionId") Long functionId, ModelMap mmap)
    {
        MdFunction mdFunction = mdFunctionService.selectMdFunctionById(functionId);
        mmap.put("mdFunction", mdFunction);
        return prefix + "/edit";
    }

    /**
     * 修改保存内置函数
     */
    @RequiresPermissions("metadata:function:edit")
    @Log(title = "内置函数", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdFunction mdFunction)
    {
        return toAjax(mdFunctionService.updateMdFunction(mdFunction));
    }

    /**
     * 删除内置函数
     */
    @RequiresPermissions("metadata:function:remove")
    @Log(title = "内置函数", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdFunctionService.deleteMdFunctionByIds(ids));
    }
}
